package com.example.websocket;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.BinaryWebSocketHandler;

@Component
public class ImageHandler extends BinaryWebSocketHandler {
    @Override
    protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
        System.out.println("receive picture from "+session.getId());
        byte[] bytes = message.getPayload().array();
        session.sendMessage(new BinaryMessage(bytes));
    }


    @Override
    public void afterConnectionEstablished(WebSocketSession session){
        session.setBinaryMessageSizeLimit(64*1024*1024);
        System.out.println("session id="+session.getId());
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        System.out.println("error");
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        System.out.println("image closed");
    }
}
